package cn.bgs.controller;


import cn.bgs.Exception.PhoenixException;
import cn.bgs.pojo.Dormitory;
import cn.bgs.pojo.VoAll.DormitoryVo;
import cn.bgs.pojo.Student;
import cn.bgs.service.DormService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Controller
@RequestMapping("/dorm")
public class DormController {

    Logger log = LogManager.getLogger();


    @Autowired
    private DormService dormService;

    //跳转
    @RequestMapping("/sel")
    public String sel(){
        return "/view/dormitoryInfo";
    }





    //添加学生
    //用于跳转页面及键值对
    @RequestMapping("/findStu/{pkId}")
    public String findStu(@PathVariable Integer pkId, Model mo){
        log.info("dormdormdormdormdormdormdorm"+pkId);
        Dormitory dorm = dormService.selectByPrimaryKey(pkId);
        mo.addAttribute("dorm",dorm);
        return "/view/addDromStudent";
    }

    //查看学生
    //跳转页面并查看宿舍内学生
    @RequestMapping("/selStu/{dormitoryId}")
    // @PathVariable 接收请求路径中占位符的值
    public String selStu(@PathVariable String dormitoryId,Model mo){
        log.info("dormstudentdormstudentdormstudent"+dormitoryId);
        mo.addAttribute("dormitoryId",dormitoryId);
        return "/view/selDormStu";

    }




    //修改 dorm
    @RequestMapping("/updateDorm")
    @ResponseBody
    public Map<String,Object> updateDorm(@RequestBody String updateDorm){
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(updateDorm);
        Dormitory dd = new Dormitory();
        dd.setPkId(jsonObject.getInteger("pkId"));
        dd.setArea(jsonObject.getString("area"));
        dd.setBuildingNo(jsonObject.getString("buildingNo"));
        dd.setBuildingFloor(jsonObject.getString("buildingFloor"));
        dd.setDormitoryId(jsonObject.getString("dormitoryId"));
        dd.setDormitoryName(jsonObject.getString("dormitoryName"));
        dd.setBedNumber(jsonObject.getInteger("bedNumber"));
        dd.setDormSupervisorId(jsonObject.getString("dormSupervisorId"));
        dd.setDormSupervisor(jsonObject.getString("dormSupervisor"));
        dd.setUseStatus(jsonObject.getString("useStatus"));
        dd.setRemark(jsonObject.getString("remark"));
        Calendar now = Calendar.getInstance();
        dd.setUpdateTime(now.getTime());

        try{
            int a = dormService.updateDorm(dd);
            map.put("data",a);
            map.put("code","0000");
            map.put("msg","修改成功");
        }catch (PhoenixException p){
            log.error("code",p.getCode());
            log.error("msg",p.getMsg());
            log.error("exception"+p.getMessage());
        }
        return map;
    }

    //添加 dorm
    @RequestMapping("/addDorm")
    @ResponseBody
    public Integer addDorm(@RequestBody String addDorm){
        JSONObject jsonObject = JSONObject.parseObject(addDorm);
        Dormitory dd = new Dormitory();
        dd.setArea(jsonObject.getString("area"));
        dd.setBuildingNo(jsonObject.getString("buildingNo"));
        dd.setBuildingFloor(jsonObject.getString("buildingFloor"));
        dd.setDormitoryId(jsonObject.getString("dormitoryId"));
        dd.setDormitoryName(jsonObject.getString("dormitoryName"));
        dd.setBedNumber(jsonObject.getInteger("bedNumber"));
        dd.setDormSupervisorId(jsonObject.getString("dormSupervisorId"));
        dd.setDormSupervisor(jsonObject.getString("dormSupervisor"));
        dd.setUseStatus(jsonObject.getString("useStatus"));
        dd.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();//获取时间
        dd.setCreateTime(now.getTime());
        dd.setUpdateTime(now.getTime());
        dd.setRemark(jsonObject.getString("remark"));

        return dormService.addDorm(dd);
    }



    //删除 dorm
    @RequestMapping("/delDorm")
    @ResponseBody
    public Integer delDorm(@RequestBody String num){
        JSONObject jsonObject = JSONObject.parseObject(num);
        int pkId = Integer.parseInt(jsonObject.getString("pkId"));
        log.info("pkId::::::::::::::::::::::::"+pkId);
        return dormService.delDorm(pkId);
    }


    //查询区域
    @RequestMapping("/findArea")
    @ResponseBody
    public List<Dormitory> findArea(){
        return dormService.findArea();
    }

    //查询 楼号
    @RequestMapping("/findBuildingNo")
    @ResponseBody
    public List<Dormitory> findBuildingNo(@RequestBody String no){
        JSONObject jsonObject = JSONObject.parseObject(no);
        String buildingNo = jsonObject.getString("no");
        log.info("楼号楼号楼号楼号楼号楼号楼号"+buildingNo);
        return dormService.findBuildingNo(buildingNo);
    }

    //查询 楼层
    @RequestMapping("/findBuildingFloor")
    @ResponseBody
    public List<Dormitory> findBuildingFloor(@RequestBody String ab){
        JSONObject jsonObject = JSONObject.parseObject(ab);
        String noo = jsonObject.getString("no");
        String fll = jsonObject.getString("floor");
        log.info("楼号楼号楼号楼号楼号楼号楼号楼号楼号楼号"+noo);
        log.info("楼层楼层楼层楼层楼层楼层楼层楼层楼层楼层"+fll);
        return dormService.findBuildingFloor(noo,fll);
    }


    //查询 dorm
    @RequestMapping("/findAllDorm")
    @ResponseBody
    public Map<String,Object> findAllStu(@RequestParam(required=false,defaultValue="1") int page,
                                         @RequestParam(required=false,defaultValue="7") int limit,
                                         DormitoryVo vo){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);

        log.info("String dormitoryName========"+vo.getDormitoryName()+"         ",
                "Integer bedNumber========"+vo.getBedNumber()+"         ",
                "String useStatus========"+vo.getUseStatus()+"         ",

                "String area========"+vo.getArea()+"         ",
                "String buildingNo========"+vo.getBuildingNo()+"         ",
                "String buildingFloor========"+vo.getBuildingFloor()
        );
        List<Student> sel = dormService.findAllDorm(vo);
        log.info("数据数据数据number  data======::::::::"+sel);
        // 使用pageInfo包装查询
        PageInfo<Student> rolePageInfo = new PageInfo<>(sel);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }

}
